import { _decorator, Node, AudioClip, Component, instantiate, Prefab } from 'cc';
import { SSS_Consts } from "../../../../Script/Data/SSS_Consts";
import { YangLeGeYang_GameData } from "../Data/YangLeGeYang_GameData";
import YangLeGeYang_GameFunction from "./YangLeGeYang_GameFunction";
import { bdlMgr } from "../../../../Script/Manager/BundleManager_S";
import YangLeGeYang_GameManager from "./YangLeGeYang_GameManager";
import YangLeGeYang_Card from "../Game/YangLeGeYang_Card";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

const { ccclass, property } = _decorator;

@ccclass('YangLeGeYangCardManager')
export default class YangLeGeYang_CardManager extends Component {
        public static Instance: YangLeGeYang_CardManager;
        CardList: YangLeGeYang_Card[] = [];
        onLoad() {
                YangLeGeYang_CardManager.Instance = this;
        }
        Init() {

        }

        public static Get_array_all_card_type() {
                const card_count = YangLeGeYang_GameData.MapData_CurrentLevel.length;//牌的总数
                let card_type_arr = YangLeGeYang_GameData.Card_types;//种类

                // console.log(`card_count----------`, card_count);
                // console.log(`card_type_arr----------`, card_type_arr);

                let en = 1// DanLeGeCi_GameData.CurrentLevelDataList[DanLeGeCi_GameData.CurrentLevel]//.en;
                let item = YangLeGeYang_GameData.Card_types.find(x => x === en)
                // console.log(`item----------`, item);


                let 变化 = false;
                let count = 0;
                let index_card_type_arr = 0;
                let array_all_card_type = [];
                for (let index = 0; index < card_count; index++) {

                        // if (变化) {
                        //     // let card_type = card_type_arr[0];
                        //     array_all_card_type.push(en);
                        // }
                        // else {
                        let card_type = card_type_arr[index_card_type_arr];
                        array_all_card_type.push(card_type);
                        // }

                        count++;
                        if (count >= 3) {
                                count = 0;
                                index_card_type_arr++;
                                if (index_card_type_arr > card_type_arr.length - 1) {
                                        index_card_type_arr = 0;
                                }

                                if (array_all_card_type.length >= card_count * 0.6) {
                                        变化 = true;
                                }
                        }
                }
                // console.log(`array_all_card_type`, array_all_card_type);

                return array_all_card_type;
        }
        static Shuffle(array: Array<any>) {
                let index = -1,
                        length = array.length,
                        lastIndex = length - 1;
                let size = length;
                while (++index < size) {
                        const rand = index + Math.floor(Math.random() * (lastIndex - index + 1))
                        const value = array[rand];
                        array[rand] = array[index];
                        array[index] = value;
                }
        }
        //    // 初始化打乱顺序
        public static Shuffle_MapData_CurrentLevel() {
                let array_all_card_type = YangLeGeYang_CardManager.Get_array_all_card_type()

                // ArrayUtils.Shuffle(array_all_card_type);
                YangLeGeYang_CardManager.Shuffle(array_all_card_type);

                for (let index = 0; index < YangLeGeYang_GameData.MapData_CurrentLevel.length; index++) {
                        const element = YangLeGeYang_GameData.MapData_CurrentLevel[index];
                        const card_type = array_all_card_type[index];
                        element.card_type = card_type;
                }
        }
        //    // 3消Check3Match
        public static XiaoChu3() {
                let temp_arr = [];
                for (let index = 0; index < YangLeGeYang_GameData.MapData_Operate.length; index++) {
                        const element = YangLeGeYang_GameData.MapData_Operate[index];
                        let card_type = element.card_type;
                        temp_arr.push(card_type);
                }
                // console.log(`temp_arr`, temp_arr);
                let arr_3 = YangLeGeYang_GameFunction.Check3Match(temp_arr);
                // console.log(`arr_3`, arr_3);
                if (arr_3.length >= 3) {
                        for (let index = arr_3.length - 1; index >= 0; index--) {
                                const element = arr_3[index];
                                // console.log(element);
                                let item = YangLeGeYang_GameData.MapData_Operate[element.index];
                                // console.log(item, item.list_index);
                                let card: YangLeGeYang_Card = YangLeGeYang_CardManager.Instance.CardList[item.list_index];
                                // console.log(card);

                                card.ToDelete();

                                {
                                        bdlMgr.getRes<Prefab>(SSS_Consts.BundleName.PREFAB, `Game/effects_001_ske`, Prefab).then(res => {
                                                if (res) {
                                                        let prefab = res;
                                                        const node_effect: Node = instantiate(prefab);
                                                        node_effect.active = true;
                                                        node_effect.parent = card.node.parent;
                                                        node_effect.setPosition(card.node.position);

                                                        setTimeout(() => {
                                                                node_effect.destroy();
                                                        }, 600);


                                                        // setTimeout(() => {
                                                        //         let root = `audio/消除`;
                                                        //         bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, root, AudioClip).then(res => {
                                                        //                 if (res) {
                                                        //                         AudioManager.Instance.playSFX(res, false);
                                                        //                         // audioEngine.setVolume(audioId, 1);
                                                        //                 }
                                                        //         });
                                                        // }, 60 * (index + 1));
                                                }
                                        });

                                }

                                // DanLeGeCi_GameData.MapData_Operate[element.index];
                                // ArrayUtils.Remove(DanLeGeCi_GameData.MapData_Operate, element.value);
                                YangLeGeYang_GameData.MapData_Operate.splice(element.index, 1);
                        }

                        // let a1 = arr_3[0]
                        // let item1 = WordList.find(e => e.key === a1.value);
                        // let mp3_name = `word/${item1.root}/${item1.key}--_us_1`;
                        // bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, mp3_name, AudioClip).then(res => {
                        //     if (res) {
                        //         audioEngine.playEffect(res, false);
                        //     }
                        // });


                        {
                                // let root = `audio/merge sound`;
                                // let r = randomRangeInt(1, 3);
                                let root = `audio/水音效1`;
                                bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, root, AudioClip).then(res => {
                                        if (res) {
                                                SSS_AudioManager.Instance.playSFX(res, false);
                                                // audioEngine.setVolume(audioId, 0.6);
                                        }
                                });
                        }

                        // AudioManager.Play_sound("clear", false, 1);
                        // if (GameStorage.data.vibrate_on) {
                        //     CommonFunction.VibrateShort();
                        // }

                        let isAllDelete: boolean = YangLeGeYang_GameFunction.CheckIsAllDelete();
                        if (isAllDelete) {
                                // TipPanel.Instance.Show(`恭喜过关~~~`);
                                // director.loadScene(SSS_Consts.SceneName.MainScene);
                                setTimeout(() => {
                                        // GameManager.Instance.PassLevel();
                                        YangLeGeYang_GameManager.Instance.GameWin();
                                }, 400);
                        }

                        return true;
                }

                // OperatePanel.Instance.RefreshAllCard();
        }
        //    // 是否死亡
        public static IsDead() {
                if (YangLeGeYang_GameData.MapData_Operate.length >= YangLeGeYang_GameData.OperatePanelGridNumber) {
                        // return true;
                        let temp_arr = [];
                        for (let index = 0; index < YangLeGeYang_GameData.MapData_Operate.length; index++) {
                                const element = YangLeGeYang_GameData.MapData_Operate[index];
                                let card_type = element.card_type;
                                temp_arr.push(card_type);
                        }

                        // console.log(`temp_arr`, temp_arr);
                        let arr_3 = YangLeGeYang_GameFunction.Check3Match(temp_arr);
                        // console.log(`arr_3`, arr_3);
                        if (arr_3.length >= 3) {
                                return false;
                        }
                        else {
                                return true;
                        }
                }
                else {
                        return false;
                }
        }
}

/** 调试 */
window["YangLeGeYang_CardManager"] = YangLeGeYang_CardManager;